﻿#include "ExecutorImpl.hpp"
#include "Command.hpp"
#include <memory>
#include <unordered_map>
namespace adas
{
    Executor* Executor::NewExecutor(const Pose& pose) noexcept
    {
        return new (std::nothrow) ExecutorImpl(pose);
    }
    ExecutorImpl::ExecutorImpl(const Pose& pose) noexcept : poseHandler(pose){}//初始化方式规范
    Pose ExecutorImpl::Query() const noexcept//状态读取函数实例化
    {
        return poseHandler.Query();
    }
    void ExecutorImpl::Execute(const std::string& commands) noexcept//外界输入接收函数实例化
    {
        const std::unordered_map<char, std::function<void(PoseHandler & poseHandler)>> cmderMap {
            {'M', MoveCommand()},//输入'M'进入前进状态
            {'L', TurnLeftCommand()},//输入'L'进入左转状态
            {'R', TurnRightCommand()},//输入'R'进入右转状态
            {'F', FastCommand()},//输入'F'进入加速状态
            {'B', ReverseCommand()},//输入'B'进入后退状态
            {'C', BusCommand()},//输入'C'进入公交车状态
        };
        for (const auto cmd : commands) {
            const auto it = cmderMap.find(cmd);
            if (it != cmderMap.end()) {
                it->second(poseHandler);
            }
        }
    }
}